<?php
namespace Common\Model;
use Common\Model\CommonModel;
class AppGroupModel extends CommonModel{
	//自动验证
	protected $_validate = array(
			//array(验证字段,验证规则,错误提示,验证条件,附加规则,验证时间)
			array('group_uniq_id', 'require', '群互动id不能为空！', 0, 'regex', 1),
			array('group_uniq_id','','群互动id已经存在！',0,'unique',1),
			array('user_uniq_id', 'require', '群主互动id不能为空！', 0, 'regex', 1),
			array('descscription', 'require', 'descscription不能为空！',0, 'regex', 1),
			array('groupname', 'require', 'groupname不能为空！', 0, 'regex', 1),
			array('is_public',array(1,2),'is_public值的范围不正确！',2,'in'),
			array('is_allowinvites',array(1,2),'is_allowinvites值的范围不正确！',2,'in'),
	);
	//自动完成
	protected $_auto = array( 
          array('icon','setIcon',1,'callback')  // 新增的时候设置头像
     );

	protected function _before_write(&$data){
		parent::_before_write($data);
	}
	/**设置头像*/
	function setIcon(){
	 	return "/data/icon/".rand(101, 109).".png";
	 }
	/**生成互动号*/
	function create_group_uniq_id(){
		return rand(10,99).substr(time(), 3);
	}
	/*群成员详情*/
	function group_user_detail($group_uniq_id='',$user_uniq_id=''){
	  $filed="a.user_uniq_id,a.group_uniq_id,a.level,a.is_accept_msg,a.is_black,b.nick,b.sex,b.grade,b.balance";
	  $data=M("UserGroup")->alias("a")->field($filed)->join(array("left join m_app_user as b on a.user_uniq_id=b.user_uniq_id"))->where("a.group_uniq_id='$group_uniq_id' and a.user_uniq_id='user_uniq_id'")->find();
	  return $data;
	}
	
	/*群成员列表*/
	function group_user_list($group_uniq_id=''){
	  $filed="a.user_uniq_id,a.group_uniq_id,a.level,a.is_accept_msg,a.is_black,b.nick,b.sex,b.grade,b.balance";
	  $list=M("UserGroup")->alias("a")->field($filed)->join(array("left join m_app_user as b on a.user_uniq_id=b.user_uniq_id"))->where("a.group_uniq_id='$group_uniq_id'")->select();
	  return $list;
	}
	
	/*我的群*/
	function my_group_list($user_uniq_id){
		 $filed="a.user_uniq_id,a.group_uniq_id,a.level,a.is_accept_msg,a.is_black,b.group_uniq_id,b.huanxing_groupid,b.groupname,b.is_recommend";
	     $list=M("UserGroup")->alias("a")->field($filed)->join(array("left join m_app_group as b on a.group_uniq_id=b.group_uniq_id"))->where("a.user_uniq_id='$user_uniq_id'")->select();
	     return $list;
	}
	
}
